<template>
  <el-card>
    <template #header> 修改密码 </template>
    <div class="UsersAdd-all">
      狠人阿凝
      <el-form
        :model="resetedForm"
        :rules="rules"
        ref="ruleForm"
        label-width="160px"
        class="demo-ruleForm"
      >
        <!-- <el-form-item label="用户id" prop="member_id">
          <el-input
            type="text"
            v-model="resetedForm.member_id"
            autocomplete="off"
            placeholder="请输入用户id"
            prefix-icon="iconfont icon-yonghu1"
          ></el-input>
        </el-form-item> -->

        <el-form-item label="旧的密码" prop="oldPassword">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="resetedForm.oldPassword"
            autocomplete="off"
            placeholder="请输入旧的密码"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont point"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              >
              </i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="新的密码" prop="password">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="resetedForm.password"
            autocomplete="off"
            placeholder="请输入新的密码"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont point"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              >
              </i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="请确认新的密码" prop="repassword">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="resetedForm.repassword"
            placeholder="请再次输入新的密码"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont point"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              >
              </i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">
            确认修改
          </el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- <div>狗曹玉杰</div> -->
  </el-card>
</template>

<script>
import { rePwd } from '@/api/users.js'
export default {
  data() {
    // var validatemember = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请输入用户名称'))
    //   } else {
    //     callback()
    //   }
    // }
    var validateoldPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧的密码'))
      } else {
        callback()
      }
    }
    var validatepassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新的密码'))
      } else if (this.resetedForm.repassword !== '') {
        // 表单的一致性规定，在验证新密码时失去焦点会触发确认密码的校验
        this.$refs.ruleForm.validateField('repassword')
      } else if (this.resetedForm.oldPassword === this.resetedForm.password) {
        callback(new Error('旧密码与新密码一致'))
      } else {
        callback()
      }
    }
    var validaterepassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新的密码'))
      } else if (value !== this.resetedForm.password) {
        callback(new Error('两次输入新的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      resetedForm: {
        // member_id: '',
        oldPassword: '',
        password: '',
        repassword: ''
      },
      rules: {
        // member_id: [{ validator: validatemember, trigger: 'blur' }],
        oldPassword: [{ validator: validateoldPassword, trigger: 'blur' }],
        password: [{ validator: validatepassword, trigger: 'blur' }],
        repassword: [{ validator: validaterepassword, trigger: 'blur' }]
      },
      flag: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 验证通过，可以修改密码
          // 获取本地数据的 token 和 member_id
          const users = JSON.parse(window.localStorage.getItem('users') || '{}')
          const { code, msg } = await rePwd({
            member_id: users.member_id,
            access_token: users.access_token,
            oldPassword: this.resetedForm.oldPassword,
            password: this.resetedForm.password,
            repassword: this.resetedForm.repassword
          })
          if (code === 1) {
            // element-ui 提供了一个页面的提示组件
            this.$message.success(msg) // 固定格式
            // 修改密码之后要重新登录
            window.localStorage.clear() // 清除本地储存的数据
            setTimeout(() => {
              location.reload() // 刷新
            }, 1500)
          } else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.flag = true
      // console.log(this.flag)
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  .UsersAdd-all {
    .el-form {
      width: 600px;
      .el-select {
        width: 440px;
      }
    }
  }
}
</style>
